package com.quanyan.ic.video.http.controller;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.common.http.APIResponse;
import com.quanyan.ic.video.http.cache.Hik8710EventDAO;
import com.quanyan.ic.video.http.service.Hik8710EventService;
import com.quanyan.ic.video.model.hik8710.Hik8710Event;
import com.quanyan.ic.video.model.hik8710.Hik8710PlaceUnitSetting;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * Created by zhuo.shi on 2017/8/16.
 */
@RestController
@RequestMapping("/larkic/placevideo")
public class Hik8710EventController {

    private static final Logger logger = LoggerFactory.getLogger(Hik8710EventController.class);

    @Autowired
    @Qualifier("131331")
    private Hik8710EventService service131331;

    @Autowired
    @Qualifier("131585")
    private Hik8710EventService service131585;

    @Autowired
    @Qualifier("131588")
    private Hik8710EventService service131588;

    @Autowired
    private Hik8710EventDAO hik8710EventDAO;

    /**
     * 接收8710平台推送数据
     */
    @RequestMapping(value = "/receiveEventLogs", method = RequestMethod.POST)
    public void receiveEventLogs(HttpServletRequest request) throws Exception {
        Enumeration em = request.getParameterNames();
        String eventStr = "";
        while (em.hasMoreElements()) {
            eventStr += (String) em.nextElement();
        }
        logger.info("receiveData:" + eventStr);
        if (StringUtils.trimAllWhitespace(eventStr).isEmpty()) {
            logger.error("event string is null!");
            return;
        }
        List<Hik8710Event> eventList = null;
        try {
            eventList = JSON.parseArray(eventStr, Hik8710Event.class);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        for (Hik8710Event hik8710Event : eventList) {
            switch (hik8710Event.getEvent_type()) {
                case 131331:
                    service131331.handleEvent(hik8710Event);
                    break;
                case 131585:
                    service131585.handleEvent(hik8710Event);
                case 131588:
                    service131588.handleEvent(hik8710Event);
                default:
                    logger.warn("unknow 8710 event {}, ", JSON.toJSONString(hik8710Event));
            }
        }
    }

    /**
     * 导入8710历史数据，测试数据可用性
     */
    @RequestMapping(value = "/receiveEventLogs/test", method = RequestMethod.POST)
    public APIResponse testReceiveEventLogs(@RequestBody Hik8710Event hik8710Event) {
        logger.info("receive Test Data:" + JSON.toJSONString(hik8710Event));
        switch (hik8710Event.getEvent_type()) {
            case 131331:
                service131331.handleEvent(hik8710Event);
                break;
            case 131585:
                service131585.handleEvent(hik8710Event);
            case 131588:
                service131588.handleEvent(hik8710Event);
            default:
                logger.warn("unknow 8710 event {}, ignore", JSON.toJSONString(hik8710Event));
        }
        return APIResponse.returnSuccess();
    }

    /**
     * 设置场与摄像头直接的映射关系
     */
    @RequestMapping("/initPlaceUnitSetting")
    public APIResponse setPlaceUnitSetting(@RequestBody List<Hik8710PlaceUnitSetting> settings) {
        hik8710EventDAO.set8710EventPlaceUnitSetting(settings);
        return APIResponse.returnSuccess();
    }

    /**
     * 设置移动侦测告警事件数据模型参数
     */
    @RequestMapping("/set131331EventDataModelParams")
    public APIResponse set131331EventDataModelParams(@RequestBody Map<String, Object> map) {
        hik8710EventDAO.set131331EventDataModelParams(MapUtils.getLong(map, "continuousInterval"), MapUtils.getLong(map, "alarmLimitTime"));
        return APIResponse.returnSuccess();
    }

    /**
     * 设置某个场馆是否拉取告警显示到前端
     */
    @RequestMapping(value = "/setPlaceAlarmSwitch")
    public APIResponse setPlaceAlarmSwitch(@RequestBody Map<String, Object> map) {
        hik8710EventDAO.setPlaceAlarmSwitch(MapUtils.getInteger(map, "placeId"), MapUtils.getBoolean(map, "sw"));
        return APIResponse.returnSuccess();
    }

    /**
     * 获取某个场馆的视频监控设置
     */
    @RequestMapping("/getPlaceVideoSetting")
    public APIResponse getPlaceVideoSetting(@RequestBody Map<String, Object> map) {
        return APIResponse.returnSuccess(hik8710EventDAO.getPlaceVideoSetting(MapUtils.getInteger(map, "placeId")));
    }
}
